#ifndef CCLIENT_H
#define CCLIENT_H
#define BOOST_THREAD_USE_LIB

#include<boost\asio.hpp>
#include<boost\bind.hpp>
#include<queue>
#include<boost\thread\mutex.hpp>
#include <boost\thread.hpp>
#include"Log.h"
#include "Packet.h"
namespace yssdk
{
#define bufferSize 2048
class CClient
{
public:
    CClient();
    bool   m_connect;
    ~CClient();
    Message* GetQueueMessage()
    {
        Message*  msg=NULL;
         m_queueMessage_mutex.lock ();
        if( !m_Messagequeue.empty ())
        {
                 msg=m_Messagequeue.front ();
                  m_Messagequeue.pop ();
        }
          m_queueMessage_mutex.unlock ();
         return msg;
    }
    void pushQueueMessage(Message* msg)
    {
        m_queueMessage_mutex.lock ();
        m_Messagequeue.push (msg);
        m_queueMessage_mutex.unlock ();
    }
    void freeQueueMessage(Message *msg)
    {
          free((unsigned char*)msg);
          msg=NULL;
    }
    void sendMessage(Message*);
    void sendpostcomplete(Message *);
    void closePostcomplete();
     void closeConnectHandl();
    void run(char* str,int port);
    void connectcomplete(boost::system::error_code error);
    unsigned char buffer[bufferSize];
    netstream *bufferstream;
    void PushStream(int size);
    bool PopMsg(unsigned char*,int size);
    unsigned char * GetMsg();
    void ZeroBuffer()
    {
        ZeroMemory(buffer,bufferSize);
    }
    void readIo();
    void readComplete(const boost::system::error_code& error,
        size_t bytes_transferred);

    void SendIo(Message*);
    void SendComplete(Message*,const boost::system::error_code& error);
private:
    boost::asio::ip::tcp::socket *m_socket;
    boost::asio::io_service m_ioservice;
    std::queue<Message*> m_Messagequeue;
    boost::mutex            m_queueMessage_mutex;
};

}
#endif // CCLIENT_H
